package com.heaven.jane.sourcegenerator.commons.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heaven.jane.sourcegenerator.commons.constant.ResultCode;
import com.heaven.jane.sourcegenerator.commons.exception.BusinessException;
import com.heaven.jane.sourcegenerator.commons.response.ResponseResult;

import lombok.extern.slf4j.Slf4j;

/**
 * controller切面
 * 
 * @author heaven
 *
 */
@Slf4j
@ControllerAdvice
public class CommonsControllerAdvice {

	@ResponseBody
	@ExceptionHandler({BusinessException.class})
	public ResponseResult handle(BusinessException ex) {
		return ResponseResult.builder().code(ex.getCode()).build();
	}

	@ResponseBody
	@ExceptionHandler({Exception.class})
	public ResponseResult handle(Exception ex) {
		log.error(CommonsControllerAdvice.class.getCanonicalName(), "system error", ex);
		return ResponseResult.builder().code(ResultCode.SYSTEM_ERROR.getCode()).build();
	}
}
